//
//  TabooDetailViewController.m
//  FruitsAndHealthy
//
//  Created by appying_wxj on 14/11/14.
//  Copyright (c) 2014年 appying_wxj. All rights reserved.
//

#import "TabooDetailViewController.h"
#import "UMSocialWechatHandler.h"// 微信
#import "UMSocialQQHandler.h"// qq
#import "UMSocial.h"
@interface TabooDetailViewController ()

@end

@implementation TabooDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    if ([AllRequest checkNet]) {
        [self initTabooDetail];
        
    }
   [self getTabooDate];
}
-(void)initTabooDetail{
    self.titleLab.text = @"详细内容";
    
    // 导航条左边的按钮
    [self.leftButton addTarget:self action:@selector(backButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    
    NSString *str = self.tabooDetailContent;//获取更新的内容
    
    
    UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 64, kScreen_Width, kScreen_Height-64)];
    [webView loadHTMLString:str baseURL:nil];
    webView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:webView];
    
    self.collectButton = [UIButton buttonWithType:UIButtonTypeCustom];
    //    [self.collectButton setBackgroundImage:[UIImage imageNamed:@"image_noCollect@2x"] forState:UIControlStateNormal];
    self.collectButton.frame = CGRectMake(kScreen_Width-70, kScreen_Height-170, 50, 50);
    [self.view addSubview:self.collectButton];
    
    self.shareButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.shareButton setBackgroundImage:[UIImage imageNamed:@"image_share@2x"] forState:UIControlStateNormal];
    [self.shareButton addTarget:self action:@selector(shareButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    self.shareButton.frame = CGRectMake(kScreen_Width-70, kScreen_Height-100, 50, 50);
     self.shareButton.showsTouchWhenHighlighted = YES;// 按钮按下时会发光
    [self.view addSubview:self.shareButton];

    
}

// 获取收藏数据
-(void)getTabooDate{
    _nameArray = [[NSMutableArray alloc] init];
    _contentArray = [[NSMutableArray alloc] init];
    // 创建表 FMDB数据存储
    [BaseFruits createTable];
    _array = [BaseFruits getAllFruitsTitle];
    if ([self.array count] == 0) {
        [self.collectButton setBackgroundImage:[UIImage imageNamed:@"image_noCollect@2x"] forState:UIControlStateNormal];
        [self.collectButton addTarget:self action:@selector(tabooCollectButtonClick:)
                     forControlEvents:UIControlEventTouchUpInside];

    }else{
        for (int i = 0 ; i < self.array.count; i++) {
            [_nameArray addObject:[_array objectAtIndex:i]];
        }
        NSMutableArray *nameArr= [[NSMutableArray alloc] initWithCapacity:0];
        for (int j = 0 ; j < _nameArray.count; j++) {
            _name = [_nameArray objectAtIndex:j];
            _nameStr= [NSString stringWithFormat:@"%@",_name.fruitsTitle];
            [nameArr addObject:_nameStr];
        }
        for (int g = 0; g < nameArr.count; g++) {
            //        NSLog(@"----------获取到的数据-------%lu",(unsigned long)nameArr.count);
            // 右边按钮的点击事件
            if ([nameArr containsObject:self.tabooDetailTitle]) {
                [self.collectButton setBackgroundImage:[UIImage imageNamed:@"image_finishCollect@2x"] forState:UIControlStateNormal];
                self.collectButton.enabled = NO;
            }else{
                [self.collectButton setBackgroundImage:[UIImage imageNamed:@"image_noCollect@2x"] forState:UIControlStateNormal];
                [self.collectButton addTarget:self action:@selector(tabooCollectButtonClick:)
                             forControlEvents:UIControlEventTouchUpInside];
            }
        }

    }
    
}
#pragma mark------------UIButtonClick--------------
-(void)backButtonClick:(UIButton *)sender{
    [self dismissViewControllerAnimated:YES completion:nil];
}
-(void)tabooCollectButtonClick:(UIButton *)sender{
    [self.collectButton setBackgroundImage:[UIImage imageNamed:@"image_finishCollect@2x"] forState:UIControlStateNormal];
    Fruits *title = [[Fruits alloc] init];
    title.fruitsTitle = self.tabooDetailTitle;
    title.fruitsContent = self.tabooDetailContent;
    [BaseFruits insertFruits:title];
    // 每次插入之后从数据库中获取所有数据
    self.array = [BaseFruits getAllFruitsTitle];
    
    self.collectButton.enabled = NO;
}
-(void)shareButtonClick:(UIButton *)sender{
    [self.shareButton setHidden:YES];
    [self.collectButton setHidden:YES];
    
    //状态提示框
    _greyView = [[UIView alloc]initWithFrame:
                 CGRectMake(0, 0, Kwidth, kheight)];
    _greyView.backgroundColor =
    [UIColor colorWithRed:70.0 / 255.0 green:70.0 / 255.0 blue:70.0 / 255.0 alpha:0.5];
    [self.view addSubview:_greyView];
    
    UIView *alertView = [[UIView alloc]initWithFrame:CGRectMake(10, kheight, 300, 100)];
    alertView.backgroundColor = [UIColor colorWithRed:0.94 green:0.94 blue:0.94 alpha:0.8];
    [alertView.layer setMasksToBounds:YES];
    [alertView.layer setCornerRadius:10.0]; //设置矩形四个圆角半径
    [alertView.layer setBorderWidth:1.0];
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    CGColorRef colorref = CGColorCreate(colorSpace,(CGFloat[]){ 0, 0, 0, 0 });
    [alertView.layer setBorderColor:colorref];//边框颜色
    
    //qq按钮
    UIButton *qqButton = [UIButton buttonWithType:UIButtonTypeCustom];
    qqButton.frame = CGRectMake(35, 30, 40, 40);
    [qqButton setImage:[UIImage imageNamed:@"image_QQ.png"] forState:UIControlStateNormal];
    [qqButton addTarget:self action:@selector(qqButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    qqButton.tag = 100;
    [alertView addSubview:qqButton];
    
    //微信按钮
    UIButton *weiXinButton = [UIButton buttonWithType:UIButtonTypeCustom];
    weiXinButton.frame = CGRectMake(100, 30, 40, 40);
    [weiXinButton setImage:[UIImage imageNamed:@"sns_icon_22.png"] forState:UIControlStateNormal];
    [weiXinButton addTarget:self action:@selector(weiXinButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    weiXinButton.tag = 101;
    [alertView addSubview:weiXinButton];
    
    //  取消的按钮
    UIButton *cancelButton = [UIButton buttonWithType:UIButtonTypeCustom];
    cancelButton.frame = CGRectMake(10, kheight+120, 300, 40);
    [cancelButton.layer setMasksToBounds:YES];
    [cancelButton.layer setCornerRadius:10.0]; //设置矩形四个圆角半径
    [cancelButton.layer setBorderWidth:1.0];
    [cancelButton.layer setBorderColor:colorref];//边框颜色
    cancelButton.backgroundColor = [UIColor whiteColor];
    [cancelButton setTitle:@"Cancel" forState:UIControlStateNormal];
    [cancelButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    cancelButton.backgroundColor = [UIColor colorWithRed:0.94 green:0.94 blue:0.94 alpha:0.8];
    [cancelButton addTarget:self action:@selector(backViewController:) forControlEvents:UIControlEventTouchUpInside];
    
    
    
    [UIView animateWithDuration:0.5 animations:^{
        alertView.frame=CGRectMake(10, kheight-170, 300, 100);
        cancelButton.frame = CGRectMake(10, kheight-60, 300, 40);
        [_greyView addSubview:cancelButton];
        [_greyView addSubview:alertView];
    }completion:^(BOOL finished) {
    }];
    
    
}
// qq分享
-(void)qqButtonClick:(UIButton *)sender{
    [[UMSocialDataService defaultDataService]  postSNSWithTypes:@[UMShareToQQ] content:@"《水果益健康》教你吃水果，巧吃水果萌萌哒！都是吃货无须解释！你倒是点我呀！https://fruithealthy.avosapps.com/share" image:nil location:nil urlResource:nil presentedController:self completion:^(UMSocialResponseEntity *response){
        if (response.responseCode == UMSResponseCodeSuccess) {
//            NSLog(@"分享成功！");
        }
    }];
}
// 微信分享
-(void)weiXinButtonClick:(UIButton *)sender{
    [[UMSocialDataService defaultDataService]  postSNSWithTypes:@[UMShareToWechatSession] content:@"《水果益健康》教你吃水果，巧吃水果萌萌哒！都是吃货无须解释！你倒是点我呀！https://fruithealthy.avosapps.com/share" image:nil location:nil urlResource:nil presentedController:self completion:^(UMSocialResponseEntity *response){
        if (response.responseCode == UMSResponseCodeSuccess) {
//            NSLog(@"分享成功！");
        }
    }];
}
// 分享页面的取消按钮
-(void)backViewController:(UIButton *)sender{
    [self.collectButton setHidden:NO];
    [self.shareButton setHidden:NO];
    [_greyView removeFromSuperview];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
